﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;
using Cms.Web.Framework.Infrastructure.Wrappers;
using Cms.Utilities;

namespace Cms.Web.Framework.Infrastructure
{
    public class RequestStart : IHttpModule
    {
        private RequestStartHandler _requestStartHandler;

        #region IHttpModule Members

        public void Dispose()
        {
        }
        
        public void Init(HttpApplication application)
        {
            application.BeginRequest += BeginRequest;
            application.AuthorizeRequest += AuthorizeRequest;
        }

        #endregion

        protected void BeginRequest(object sender, EventArgs e)
        {
            // Create the wrapper objects and the handler
            ICmsHttpRequest request = new CmsHttpRequest(HttpContext.Current.Request);
            ICmsHttpResponse response = new CmsHttpResponse(HttpContext.Current.Response);
            ICmsIdentity identity = new CmsIdentity();
            ICmsFormsAuthentication formsAuthentication = new CmsFormsAuthentication();
            ICmsHttpContextItems contextItems = new CmsHttpContextItems();
            _requestStartHandler = new RequestStartHandler(request, response, identity, formsAuthentication, contextItems);

            // Fire the event wrapper
            _requestStartHandler.BeginRequest();
        }
        
        protected void AuthorizeRequest(object sender, EventArgs e)
        {
            _requestStartHandler.AuthorizeRequest();
        }
    }
}